package cn.fancylab.template.web.action.manager.devcie;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.fancylab.model.acl.AuthUser;
import cn.fancylab.model.bs.DeviceController;
import cn.fancylab.service.DaoService;
import cn.fancylab.template.util.JsonUtil;
import cn.fancylab.template.vo.TableListVo;
import cn.fancylab.template.web.action.admin.BaseAdminController;
import cn.fancylab.web.component.acl.SecurityUtil;
import cn.fancylab.web.support.FancyVelocityView;
import cn.fancylab.web.support.JsonStrDirectOut;

@Controller
public class ControllerController extends BaseAdminController implements InitializingBean {
	@Autowired
	@Qualifier("daoService")
	private DaoService daoService;

	@RequestMapping(value = "/manager/device/controller/list.xhtml")
	public String list(ModelMap model) {
		return "manager/device/controller/list.vm";
	}

	@RequestMapping(value = "/manager/device/controller/list_data.xhtml")
	public String list_data(ModelMap model) {
		HttpServletRequest request = getRequest();

		int pagesize = 10;
		int page = 0;
		String currentpage = request.getParameter("page");
		String limit = request.getParameter("limit");
		String identified_no = request.getParameter("identified_no");

		if (StringUtils.isNotBlank(currentpage)) {
			page = Integer.valueOf(currentpage) - 1;
		}
		if (StringUtils.isNotBlank(limit)) {
			pagesize = Integer.valueOf(limit);
		}
		DetachedCriteria query = DetachedCriteria.forClass(DeviceController.class);
		query.add(Restrictions.eq("is_del", 0));
		if (StringUtils.isNotBlank(identified_no)) {
			query.add(Restrictions.like("identified_no", "%".concat(identified_no).concat("%")));
		}
		int count = daoService.getObjectCountByCriteria(query);
		query.setProjection(null);
		List<DeviceController> list = daoService.findByCriteria(query, page * pagesize, pagesize);

		TableListVo<DeviceController> tableListVo = new TableListVo<DeviceController>();
		tableListVo.setCode(0);
		tableListVo.setMsg("");
		tableListVo.setCount(count + "");
		tableListVo.setData(list);

		request.setAttribute(FancyVelocityView.RENDER_JSON, "true");
		request.setAttribute(FancyVelocityView.DIRECT_OUTPUT,
				new JsonStrDirectOut(getResponse(), JsonUtil.serialize(tableListVo)));
		return "common/directOut.vm";
	}

	@RequestMapping(value = "/manager/device/controller/add.xhtml")
	public String add(String uuid) {
		return "manager/device/controller/form.vm";
	}

	@RequestMapping(value = "/manager/device/controller/edit.xhtml")
	public String edit(String uuid, ModelMap model) {
		DeviceController deviceController = daoService.getObject(DeviceController.class, uuid);
		model.put("deviceController", deviceController);
		return "manager/device/controller/form.vm";
	}

	@RequestMapping(value = "/manager/device/controller/save.xhtml")
	public String save(DeviceController deviceControllerParameter) {
		AuthUser loginUser = SecurityUtil.getLogonUser();

		DeviceController deviceController = daoService.getObject(DeviceController.class,
				deviceControllerParameter.getUuid());

		if (null == deviceController) {
			deviceController = new DeviceController();
			deviceController.setUuid(UUID.randomUUID().toString());
			deviceController.setIs_del(0);
			deviceController.setAdd_date(new Date());
			deviceController.setAdd_user_id(loginUser.getId());
		} else {
			deviceController.setUp_date(new Date());
			deviceController.setUp_user_id(loginUser.getId());
		}
		deviceController.setIdentified_no(deviceControllerParameter.getIdentified_no());
		deviceController.setIp_address(deviceControllerParameter.getIp_address());
		deviceController.setUdp_port(deviceControllerParameter.getUdp_port());
		deviceController.setSocket_port(deviceControllerParameter.getSocket_port());
		deviceController.setUdp_server_state(deviceControllerParameter.getUdp_server_state());
		deviceController.setSocket_server_state(deviceControllerParameter.getSocket_server_state());
		deviceController.setOpenfire_password(deviceControllerParameter.getOpenfire_password());
		deviceController.setOpenfire_username(deviceControllerParameter.getOpenfire_username());
		deviceController.setController_location(deviceControllerParameter.getController_location());
		deviceController.setController_name(deviceControllerParameter.getController_name());
		deviceController.setEntp_id(deviceControllerParameter.getEntp_id());
		deviceController.setEntp_name(deviceControllerParameter.getEntp_name());

		daoService.saveObject(deviceController);

		HttpServletRequest request = getRequest();
		request.setAttribute(FancyVelocityView.RENDER_JSON, "true");
		request.setAttribute(FancyVelocityView.DIRECT_OUTPUT,
				new JsonStrDirectOut(getResponse(), JsonUtil.serialize(deviceController)));
		return "common/directOut.vm";
	}

	@RequestMapping(value = "/manager/device/controller/del.xhtml")
	public String del(String[] uuid) {
		AuthUser loginUser = SecurityUtil.getLogonUser();

		DeviceController deviceController;
		for (int i = 0, len = uuid.length; i < len; i++) {
			if (StringUtils.isNotBlank(uuid[i])) {
				deviceController = daoService.getObject(DeviceController.class, uuid[i]);

				if (null != deviceController) {
					deviceController.setIs_del(1);
					deviceController.setDel_date(new Date());
					deviceController.setDel_user_id(loginUser.getId());
					daoService.saveObject(deviceController);
				}
			}
		}

		HttpServletRequest request = getRequest();
		request.setAttribute(FancyVelocityView.RENDER_JSON, "true");
		request.setAttribute(FancyVelocityView.DIRECT_OUTPUT,
				new JsonStrDirectOut(getResponse(), JsonUtil.serialize(new HashMap())));
		return "common/directOut.vm";
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub

	}
}
